package org.base.core.utils.ftp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class FtpUtils {
	
	/**
	 * 
	 * @param host FTP服务器IP地址
	 * @param port FTP服务器端口号
	 * @param username FTP服务器登录用户名
	 * @param password FTP服务器登录密码
	 * @param remotePath FTP服务器上的文件存放路径
	 * @param filename  下载的文件名
	 * @param localPath 下载到本地的路径
	 * @return
	 */
	public static boolean dowloadFile(String host,int port,String username,String password,String remotePath,String filename,String localPath) {
		boolean result = false;
		FTPClient client = new FTPClient();
		try {
			client.connect(host, port);
			client.login(username, password);
			int reply = client.getReplyCode();
			if(!FTPReply.isPositiveCompletion(reply)) {
				return result;
			}
			client.changeWorkingDirectory(remotePath);
			FTPFile[] files = client.listFiles();
			for(FTPFile file : files) {
				if(file.getName().equals(filename)) {
					File localFilePath = new File(localPath);
					if(!localFilePath.exists()) {
						if(!localFilePath.mkdirs()) {
							return result;
						}
					}
					OutputStream out = new FileOutputStream(new File(localPath+"/"+filename));
					client.retrieveFile(filename, out);
					out.close();
				}
			}
			client.logout();
			result = true;
		}catch (IOException e) {
			e.printStackTrace();
			if(client.isConnected()) {
				try {
					client.disconnect();
				} catch (IOException e1) {
				}
			}
		}
		
		return result;
	}
	/**
	 * 
	 * @param host FTP服务器IP地址
	 * @param port FTP服务器端口号
	 * @param username FTP服务器登录用户名
	 * @param password FTP服务器登录密码
	 * @param filePath	上传文件的存放的相对路径，绝对路径为basePath+filePath
	 * @param filename	上传文件的存放名字
	 * @param input 上传文件的输入流
	 * @return
	 */
	public static boolean uploadFile(String host, int port, String username, String password,
			String filePath, String filename, InputStream input) {
		boolean result = false;
		FTPClient client = new FTPClient();
		try {
			client.connect(host, port);
			client.login(username, password);
			int reply = client.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				return result;
			}
			String[] pwd = client.doCommandAsStrings("pwd", "");
			String current = "/";
			if(pwd!=null&&pwd.length>0) {
				Pattern pattern = Pattern.compile("\"([\\w/]+)\"");
				Matcher matcher = pattern.matcher(pwd[0]);
				if(matcher.find()) {
					current = matcher.group(1);
				}
			}
			client.changeWorkingDirectory(current);
			// 切换上传目录
			if (!client.changeWorkingDirectory("./"+filePath)) {
				String[] dirs = filePath.split("/");
				for (String dir : dirs) {
					if (null == dir || "".equals(dir))
						continue;
					if (!client.changeWorkingDirectory("./"+dir)) {
						if (!client.makeDirectory(dir)) {
							return result;
						} else {
							client.changeWorkingDirectory("./"+dir);
						}
					}
				}
			}
			// 设置文件的上传类型为二进制类型
			client.setFileType(FTP.BINARY_FILE_TYPE);
			client.enterLocalPassiveMode();
			if (!client.storeFile(filename, input)) {
				
				return result;
			}
			result = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			
			if (client.isConnected()) {
				try {
					client.logout();
					client.disconnect();
				} catch (IOException e) {
				}
			}
		}
		return result;
	}

}
